package com.etc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.PageInfo;
import com.etc.entity.Singger;
import com.etc.service.SinggerService;
import com.etc.service.impl.SinggerServiceImpl;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class SinggerServlet
 */
@MultipartConfig
@WebServlet("/singger")
public class SinggerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private SinggerService singgerService=new SinggerServiceImpl();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SinggerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 接收请求参数op,用于区分不同的请求
				String op = request.getParameter("op");
				if ("pageByCondition".equals(op)) {
					doFindByCondition(request, response);
				}else if ("pageByUserId".equals(op)) {
					doFindByUserId(request, response);
				}
	}

	private void doFindByUserId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 接收请求参数
		String userId = request.getParameter("userId");
		String pageNum = request.getParameter("pageNum");
		// 调用业务层
		List<Singger> singgerList = singgerService.findSinggerByCondition(Integer.parseInt(userId));
		// 响应
		// 利用第三方库jackson将Java对象转换成JSON格式
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(singgerList);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	private void doFindByCondition(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 接收请求参数
		String initials = request.getParameter("initials");
		String regionName = request.getParameter("regionName");
		String gender = request.getParameter("gender"); 
		String styleName = request.getParameter("styleName");
		String pageNum = request.getParameter("pageNum");
		int pageSize = 20;
		// 调用业务层
		PageInfo<Singger> pageInfo = singgerService.findSinggerByCondition(initials, regionName, gender, styleName,Integer.parseInt(pageNum), pageSize);
		
		// 响应
		// 利用第三方库jackson将Java对象转换成JSON格式
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(pageInfo);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		
		
	}

}
